package crm.view.main;

import crm.view.Employee.EmployeeBasicInfo;
import crm.view.Employee.EmployeePerformance;
import crm.view.Employee.EmployeePlan;
import crm.view.LoginPage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;

public class EmployeeMain implements ActionListener, Runnable {
    JFrame frame = null;
    JMenuBar menuBar = null;
    JMenuItem manageMenu = null;
    JMenuItem checkMenu = null;
    JMenuItem lockMenu = null;
    JMenuItem leave = null;

    JMenu basic = null;
    JMenu others = null;
    JPanel panel = null;
    JLabel label = null;

    JMenu time = null;
    JMenuItem stop = null;
    JMenuItem over = null;

    JTextField text;
    Timer time2;


    public static void main(String[] args) {
        EmployeeMain employeeMain = new EmployeeMain();
        new Thread(employeeMain).start();
    }

    public EmployeeMain() {
        init();
        frame.setBounds(680, 300, 1096, 776);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setJMenuBar(menuBar);
        frame.setVisible(true);
    }

    public void init() {
        frame = new JFrame("welcome,公司职员!");
        panel = new JPanel();
        menuBar = new JMenuBar();
        menuBar.setBounds(0, 0, 30, 100);
        manageMenu = new JMenuItem("查询工作计划");
        checkMenu = new JMenuItem("查询基本信息");
        lockMenu = new JMenuItem("查看业绩情况");
        basic = new JMenu("员工功能");
        others = new JMenu("其他功能");
        leave = new JMenuItem("退出");

        time = new JMenu("计时功能");
        stop = new JMenuItem("暂停");
        over = new JMenuItem("继续");

        time2 = new Timer(1000, this); //WindowTime对象做计时器的监视器
        text = new JTextField(10);
        text.setBounds(800, 50, 200, 50);
        text.setEditable(false);
        frame.add(text);


        time.add(stop);
        time.addSeparator();
        time.add(over);


        label = new JLabel("职员管理页面");
        label.setFont(new Font("Dialog", 1, 30));
        label.setBounds(450, 20, 300, 60);
        frame.add(label);

        Font font1 = new Font("宋体", Font.BOLD, 30);
        basic.setFont(font1);
        others.setFont(font1);
        time.setFont(font1);

        ImageIcon icon1 = new ImageIcon("src/img/业绩.png");
        lockMenu.setIcon(icon1);
        ImageIcon icon2 = new ImageIcon("src/img/工作计划.png");
        manageMenu.setIcon(icon2);
        ImageIcon icon3 = new ImageIcon("src/img/查询.png");
        checkMenu.setIcon(icon3);
        ImageIcon icon4 = new ImageIcon("src/img/退出.png");
        leave.setIcon(icon4);
        ImageIcon icon5 = new ImageIcon("src/img/员工.png");
        basic.setIcon(icon5);
        ImageIcon icon6 = new ImageIcon("src/img/其他.png");
        others.setIcon(icon6);
        ImageIcon icon10 = new ImageIcon("src/img/计时.png");
        time.setIcon(icon10);
        ImageIcon icon8 = new ImageIcon("src/img/暂停.png");
        stop.setIcon(icon8);
        ImageIcon icon9 = new ImageIcon("src/img/结束.png");
        over.setIcon(icon9);

        menuBar.add(basic);
        basic.add(manageMenu);
        basic.addSeparator();       // 添加一条分割线
        basic.add(checkMenu);
        basic.addSeparator();       // 添加一条分割线
        basic.add(lockMenu);

        menuBar.add(time);
        menuBar.add(others);
        others.add(leave);


        ImageIcon icon = new ImageIcon("src/img/员工.jpg");
        JLabel imgLabel = new JLabel(icon);
        imgLabel.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
        panel.add(imgLabel);
        frame.add(panel);

        Font font2 = new Font("宋体", Font.BOLD, 25);
        manageMenu.setFont(font2);
        checkMenu.setFont(font2);
        lockMenu.setFont(font2);
        leave.setFont(font2);
        stop.setFont(font2);
        over.setFont(font2);
        text.setFont(font2);


        manageMenu.addActionListener(this);
        checkMenu.addActionListener(this);
        lockMenu.addActionListener(this);
        leave.addActionListener(this);


        stop.addActionListener(this);
        over.addActionListener(this);
    }

    @Override
    public void run() {
        time2.start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == time2) {
            LocalTime dateTime = LocalTime.now();
            String timeStr =
                    dateTime.getHour() + ":" + dateTime.getMinute() + ":" + dateTime.getSecond();
            text.setText(timeStr);
        } else if ("暂停".equals(e.getActionCommand()))
            time2.stop();
        else if ("继续".equals(e.getActionCommand()))
            time2.restart();

        else if ("查询工作计划".equals(e.getActionCommand())) {
            new EmployeePlan();
        } else if ("查看业绩情况".equals(e.getActionCommand())) {
            new EmployeePerformance();
        } else if ("查询基本信息".equals(e.getActionCommand())) {
            new EmployeeBasicInfo();
        } else if ("退出".equals(e.getActionCommand())) {
            frame.dispose();
            new LoginPage();
        }
    }


}
